﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using BPM.Foundation.WFService;
using BPM.Foundation.WFDB;
using System.IO;
using System.Xml;
using System.Text;
using BPM.Base.Tools;
using System.Data;
using BPM.Base.DataTypeLib;
using BPM.Base.WFLib.Designer;

namespace BPM.Base.WFLib
{
    [System.Drawing.ToolboxBitmap(typeof(初始化), "pic.callchild.png")]
    [Designer(typeof(CallWorkflowDesigner), typeof(IDesigner))]
	public  class 调用子流程:Activity
    {
        #region 1 基本信息

        //[1.1 子流程模板编号]
        private string _子流程模板编号 = "";
        [DescriptionAttribute("子流程模板编号")]
        [CategoryAttribute("1 基本信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 子流程模板编号
        {
            get
            {
                return _子流程模板编号;
            }

            set
            {
                this._子流程模板编号 = value;
            }
        }
        //--

        //[1.2 子流程模板版本]
        private string _子流程模板版本 = "";
        [DescriptionAttribute("子流程模板版本")]
        [CategoryAttribute("1 基本信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 子流程模板版本
        {
            get
            {
                return _子流程模板版本;
            }

            set
            {
                _子流程模板版本 = value;
            }
        }
        //--

        //[1.3 流程回归组]
        public static DependencyProperty 流程回归组Property = DependencyProperty.Register("流程回归组", typeof(string), typeof(调用子流程),new PropertyMetadata("group1"));

        [DescriptionAttribute("流程回归组")]
        [CategoryAttribute("1 基本信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 流程回归组
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.流程回归组Property)));
            }
            set
            {
                base.SetValue(调用子流程.流程回归组Property, value);
            }
        }

        //--

        //[1.4 回归主流程]
        public static DependencyProperty 回归主流程Property = DependencyProperty.Register("回归主流程", typeof(bool), typeof(调用子流程), new PropertyMetadata(false));

        [DescriptionAttribute("回归主流程")]
        [CategoryAttribute("1 基本信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public bool 回归主流程
        {
            get
            {
                return ((bool)(base.GetValue(调用子流程.回归主流程Property)));
            }
            set
            {
                base.SetValue(调用子流程.回归主流程Property, value);
            }
        }
        //--

        //[1.5 主流程编号]
        public static DependencyProperty 主流程编号Property = DependencyProperty.Register("主流程编号", typeof(Guid), typeof(调用子流程),new PropertyMetadata(Guid.Empty));

        [DescriptionAttribute("主流程编号")]
        [CategoryAttribute("1 基本信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public Guid 主流程编号
        {
            get
            {
                return ((Guid)(base.GetValue(调用子流程.主流程编号Property)));
            }
            set
            {
                base.SetValue(调用子流程.主流程编号Property, value);
            }
        }
        //--
        #endregion

        #region 2 业务信息
        //[2.1 业务类型]
        public static DependencyProperty 业务类型Property = DependencyProperty.Register("业务类型", typeof(string), typeof(调用子流程), new PropertyMetadata(""));

        [DescriptionAttribute("业务类型")]
        [CategoryAttribute("2 业务信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 业务类型
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.业务类型Property)));
            }
            set
            {
                base.SetValue(调用子流程.业务类型Property, value);
            }
        }
        //--

        //[2.2 业务编号]
        public static DependencyProperty 业务编号Property = DependencyProperty.Register("业务编号", typeof(string), typeof(调用子流程), new PropertyMetadata(""));

        [DescriptionAttribute("业务编号")]
        [CategoryAttribute("2 业务信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 业务编号
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.业务编号Property)));
            }
            set
            {
                base.SetValue(调用子流程.业务编号Property, value);
            }
        }
        //--

        //[2.3 主业务编号]
        public static DependencyProperty 主业务编号Property = DependencyProperty.Register("主业务编号", typeof(string), typeof(调用子流程), new PropertyMetadata(""));

        [DescriptionAttribute("主业务编号")]
        [CategoryAttribute("2 业务信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 主业务编号
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.主业务编号Property)));
            }
            set
            {
                base.SetValue(调用子流程.主业务编号Property, value);
            }
        }
        //--

        //[2.4 业务名称]
        public static DependencyProperty 业务名称Property = DependencyProperty.Register("业务名称", typeof(string), typeof(调用子流程), new PropertyMetadata(""));

        [DescriptionAttribute("业务名称")]
        [CategoryAttribute("2 业务信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 业务名称
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.业务名称Property)));
            }
            set
            {
                base.SetValue(调用子流程.业务名称Property, value);
            }
        }
        //--

        //[2.5 业务描述]
        public static DependencyProperty 业务描述Property = DependencyProperty.Register("业务描述", typeof(string), typeof(调用子流程), new PropertyMetadata(""));

        [DescriptionAttribute("业务描述")]
        [CategoryAttribute("2 业务信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 业务描述
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.业务描述Property)));
            }
            set
            {
                base.SetValue(调用子流程.业务描述Property, value);
            }
        }
        //--

        //[2.6 业务紧急度]
        public static DependencyProperty 业务紧急度Property = DependencyProperty.Register("业务紧急度", typeof(string), typeof(调用子流程), new PropertyMetadata(""));

        [DescriptionAttribute("业务紧急度")]
        [CategoryAttribute("2 业务信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 业务紧急度
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.业务紧急度Property)));
            }
            set
            {
                base.SetValue(调用子流程.业务紧急度Property, value);
            }
        }
        //--

        //[2.7 业务保密度]
        public static DependencyProperty 业务保密度Property = DependencyProperty.Register("业务保密度", typeof(string), typeof(调用子流程), new PropertyMetadata(""));

        [DescriptionAttribute("业务保密度")]
        [CategoryAttribute("2 业务信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 业务保密度
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.业务保密度Property)));
            }
            set
            {
                base.SetValue(调用子流程.业务保密度Property, value);
            }
        }
        //--

        //[2.8 启动人员]
        public static DependencyProperty 启动人员Property = DependencyProperty.Register("启动人员", typeof(string), typeof(调用子流程), new PropertyMetadata(""));

        [DescriptionAttribute("启动人员")]
        [CategoryAttribute("2 业务信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 启动人员
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.启动人员Property)));
            }
            set
            {
                base.SetValue(调用子流程.启动人员Property, value);
            }
        }
        //--


        //[2.9 启动部门]
        public static DependencyProperty 启动部门Property = DependencyProperty.Register("启动部门", typeof(string), typeof(调用子流程), new PropertyMetadata(""));

        [DescriptionAttribute("启动部门")]
        [CategoryAttribute("2 业务信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 启动部门
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.启动部门Property)));
            }
            set
            {
                base.SetValue(调用子流程.启动部门Property, value);
            }
        }
        //--

        //[2.10 启动职能]
        public static DependencyProperty 启动职能Property = DependencyProperty.Register("启动职能", typeof(string), typeof(调用子流程), new PropertyMetadata(""));

        [DescriptionAttribute("启动职能")]
        [CategoryAttribute("2 业务信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 启动职能
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.启动职能Property)));
            }
            set
            {
                base.SetValue(调用子流程.启动职能Property, value);
            }
        }
        //--


        //[2.11 启动时间]
        public static DependencyProperty 启动时间Property = DependencyProperty.Register("启动时间", typeof(DateTime), typeof(调用子流程), new PropertyMetadata(new DateTime(1900,1,1)));

        [DescriptionAttribute("启动时间")]
        [CategoryAttribute("2 业务信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public DateTime 启动时间
        {
            get
            {
                return ((DateTime)(base.GetValue(调用子流程.启动时间Property)));
            }
            set
            {
                base.SetValue(调用子流程.启动时间Property, value);
            }
        }
        //--

        //[2.12 测试编号]
        public static DependencyProperty 测试编号Property = DependencyProperty.Register("测试编号", typeof(int), typeof(调用子流程), new PropertyMetadata(0));

        [DescriptionAttribute("测试编号")]
        [CategoryAttribute("2 业务信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public int 测试编号
        {
            get
            {
                return ((int)(base.GetValue(调用子流程.测试编号Property)));
            }
            set
            {
                base.SetValue(调用子流程.测试编号Property, value);
            }
        }
        //--


        #endregion

        #region 3 权限信息
        //[3.1 管理部门]
        public static DependencyProperty 管理部门Property = DependencyProperty.Register("管理部门", typeof(string), typeof(调用子流程), new PropertyMetadata("none"));

        [DescriptionAttribute("管理部门")]
        [CategoryAttribute("3 权限信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 管理部门
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.管理部门Property)));
            }
            set
            {
                base.SetValue(调用子流程.管理部门Property, value);
            }
        }
        //--

        //[3.2 管理职能]
        public static DependencyProperty 管理职能Property = DependencyProperty.Register("管理职能", typeof(string), typeof(调用子流程), new PropertyMetadata("none"));

        [DescriptionAttribute("管理职能")]
        [CategoryAttribute("3 权限信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 管理职能
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.管理职能Property)));
            }
            set
            {
                base.SetValue(调用子流程.管理职能Property, value);
            }
        }
        //--

        //[3.3 管理人员]
        public static DependencyProperty 管理人员Property = DependencyProperty.Register("管理人员", typeof(string), typeof(调用子流程), new PropertyMetadata("none"));

        [DescriptionAttribute("管理人员")]
        [CategoryAttribute("3 权限信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 管理人员
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.管理人员Property)));
            }
            set
            {
                base.SetValue(调用子流程.管理人员Property, value);
            }
        }
        //--

        //[3.4 查看部门]
        public static DependencyProperty 查看部门Property = DependencyProperty.Register("查看部门", typeof(string), typeof(调用子流程), new PropertyMetadata("none"));

        [DescriptionAttribute("查看部门")]
        [CategoryAttribute("3 权限信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 查看部门
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.查看部门Property)));
            }
            set
            {
                base.SetValue(调用子流程.查看部门Property, value);
            }
        }
        //--

        //[3.5 查看职能]
        public static DependencyProperty 查看职能Property = DependencyProperty.Register("查看职能", typeof(string), typeof(调用子流程), new PropertyMetadata("none"));

        [DescriptionAttribute("查看职能")]
        [CategoryAttribute("3 权限信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 查看职能
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.查看职能Property)));
            }
            set
            {
                base.SetValue(调用子流程.查看职能Property, value);
            }
        }
        //--

        //[3.6 查看人员]
        public static DependencyProperty 查看人员Property = DependencyProperty.Register("查看人员", typeof(string), typeof(调用子流程), new PropertyMetadata("none"));

        [DescriptionAttribute("查看人员")]
        [CategoryAttribute("3 权限信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 查看人员
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.查看人员Property)));
            }
            set
            {
                base.SetValue(调用子流程.查看人员Property, value);
            }
        }
        //--

        //[3.7 启动人员特殊权限]
        public static DependencyProperty 启动人员特殊权限Property = DependencyProperty.Register("启动人员特殊权限", typeof(string), typeof(调用子流程), new PropertyMetadata("none"));

        [DescriptionAttribute("启动人员特殊权限")]
        [CategoryAttribute("3 权限信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 启动人员特殊权限
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.启动人员特殊权限Property)));
            }
            set
            {
                base.SetValue(调用子流程.启动人员特殊权限Property, value);
            }
        }
        //--

        //[3.8 办理时限]
        public static DependencyProperty 办理时限Property = DependencyProperty.Register("办理时限", typeof(int), typeof(调用子流程), new PropertyMetadata(-1));

        [DescriptionAttribute("办理时限")]
        [CategoryAttribute("3 权限信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public int  办理时限
        {
            get
            {
                return ((int )(base.GetValue(调用子流程.办理时限Property)));
            }
            set
            {
                base.SetValue(调用子流程.办理时限Property, value);
            }
        }
        //--

        //[3.9 超时操作]
        public static DependencyProperty 超时操作Property = DependencyProperty.Register("超时操作", typeof(string), typeof(调用子流程), new PropertyMetadata("none"));

        [DescriptionAttribute("超时操作")]
        [CategoryAttribute("3 权限信息")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 超时操作
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.超时操作Property)));
            }
            set
            {
                base.SetValue(调用子流程.超时操作Property, value);
            }
        }
        //--
        #endregion 

        #region 4 下一状态办理人员
        //[4.1 下一状态办理人员]
        public static DependencyProperty 下一状态办理人员Property = DependencyProperty.Register("下一状态办理人员", typeof(string), typeof(调用子流程), new PropertyMetadata("none"));

        [DescriptionAttribute("下一状态办理人员")]
        [CategoryAttribute("4 下一状态办理人员")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 下一状态办理人员
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.下一状态办理人员Property)));
            }
            set
            {
                base.SetValue(调用子流程.下一状态办理人员Property, value);
            }
        }
        //--
        #endregion

        #region 5 数据表单
        //[5.1 数据表单]
        public static DependencyProperty 数据表单Property = DependencyProperty.Register("数据表单", typeof(string), typeof(调用子流程), new PropertyMetadata(""));

        [DescriptionAttribute("数据表单")]
        [CategoryAttribute("5 数据表单")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 数据表单
        {
            get
            {
                return ((string)(base.GetValue(调用子流程.数据表单Property)));
            }
            set
            {
                base.SetValue(调用子流程.数据表单Property, value);
            }
        }
        //--
        #endregion

        #region 6 方法
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            //[1 调用创建流程 ]
            WorkflowService service = executionContext.GetService(typeof(WorkflowService)) as WorkflowService;
            if (service != null)
            {

               //业务信息
               BusinessParameter bParameter=new BusinessParameter();
               bParameter.businessDescription = this.业务描述;
               bParameter.businessID = this.业务编号;
               bParameter.businessName = this.业务名称;
               bParameter.businessType = this.业务类型;
               bParameter.masterBusinessID = this.主业务编号;
               bParameter.secrecy = this.业务保密度;
               bParameter.startDepartment = this.启动部门;
               bParameter.startDuty = this.启动职能;
               bParameter.startTime = DateTime.Now;
               bParameter.startUser = this.启动人员;
               if (this.测试编号 < 0)
               {
                   bParameter.testID = 0;
               }
               else
               {
                   bParameter.testID = this.测试编号;
               }
               bParameter.urgency = this.业务紧急度;

                //子流程信息
               ChildWorkflowParameter childParameter = new ChildWorkflowParameter();
               childParameter.masterInstanceID = this.主流程编号;
               childParameter.regress = this.回归主流程;
               childParameter.regressGroup = this.流程回归组;

                //权限信息
               PowerParameter pwParameter = new PowerParameter();
               pwParameter.manageDepartment = this.管理部门;
               pwParameter.manageDuty = this.管理职能;
               pwParameter.manageUser = this.管理人员;
               pwParameter.overtimeOperate = this.超时操作;
               pwParameter.seeDepartment = this.查看部门;
               pwParameter.seeDuty = this.查看职能;
               pwParameter.seeUser = this.查看人员;
               pwParameter.startUserPower = this.启动人员特殊权限;
               pwParameter.transactTimelimit = this.办理时限;
               

                //流程信息
               WorkflowStartParameter wfParam=new WorkflowStartParameter();
               wfParam.businessParameter = bParameter;
               wfParam.childWorkflowParameter = childParameter;
               if (string.IsNullOrEmpty( this.数据表单))
               {
                   wfParam.dataForm = null;
               }
               else
               {
                   StringReader stream = null;
                   XmlTextReader reader = null;


                   DataForm myDataForm = new DataForm();

                   //把数据表单字符串读取到StringReader
                   stream = new StringReader(this.数据表单);

                   //从stream装载到XmlTextReader

                   reader = new XmlTextReader(stream);


                   //把xmlTextReader 读到 dataSet
                   try
                   {
                       myDataForm.ReadXml(reader);
                   }
                   catch
                   {
                       myDataForm = null;
                   }

                   wfParam.dataForm = myDataForm;
               }
               wfParam.nextStateUserNo = this.下一状态办理人员;
               wfParam.powerParameter = pwParameter;
               wfParam.templateEdition = this.子流程模板版本;
               wfParam.templateID = this.子流程模板编号;
               
              


               service.createWorkflow("",wfParam );
            }
            else
            {

            }

            //[end]
            return base.Execute(executionContext);
        }
       

        #endregion
    }
}
